package com.qkl.xiatang.app.service.pond;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.entity.pond.PondTemplateBind;
import com.qkl.xiatang.app.mapper.PondTemplateBindMapper;
import com.qkl.xiatang.app.service.impl.AppThreadPoolService;
import com.qkl.xiatang.app.utils.AppUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PondTemplateBindService {

    @Autowired
    private AppThreadPoolService appThreadPool;

    @Autowired
    private PondTemplateBindMapper pondTemplateBindMapper;

    public String queryTemplateId(String pondId){
        PondTemplateBind entity = pondTemplateBindMapper.selectOne(new QueryWrapper<PondTemplateBind>()
                                                                    .eq("pond_id",pondId)
                                                                    .last("limit 1"));
        if(null == entity){
            return null;
        }
        return entity.getTemplateId();
    }

    public void pondTemplateBindAsynch(String companyId, String templateId, List<String> pondIdList){
        appThreadPool.execute(()->{
            pondTemplateBind(companyId,templateId,pondIdList);
        });
    }

    private void pondTemplateBind(String companyId, String templateId, List<String> pondIdList){
        if(ObjectUtils.isEmpty(pondIdList)){
            return;
        }
        List<PondTemplateBind> list = new ArrayList<>();
        for (String pondId : pondIdList){
            PondTemplateBind entity = new PondTemplateBind();
            entity.setId(AppUtils.genIdStr());
            entity.setCompanyId(companyId);
            entity.setPondId(pondId);
            entity.setTemplateId(templateId);
            list.add(entity);
        }
        pondTemplateBindMapper.pondTemplateBind(list);
    }


}
